const API_BASE_URL = '/api';

const api = {
    login: async (data) => {
        const response = await fetch(`${API_BASE_URL}/user/login`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(data)
        });

        if (!response.ok) {
            throw new Error('网络响应异常');
        }

        return response.json();
    },

    register: async (data) => {
        const response = await fetch(`${API_BASE_URL}/user/register`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                username: data.username,
                password: data.password,
                phoneNumber: data.phone
            })
        });

        if (!response.ok) {
            throw new Error('网络响应异常');
        }

        return response.json();
    }
};